package ch03.factory;

import ch03.Impl.IntermediaryInvocationHander;

import java.lang.reflect.Proxy;

public class IntermediaryJdkProxyFactory {
    /**
     * 工厂方法
     */
    public static <T> T carate(Object target){
        //IntermediaryInvocationHander 的成员变量target存在线程安全问题
        //故将IntermediaryInvocationHander作为局部变量使用。
        IntermediaryInvocationHander handler = new IntermediaryInvocationHander();
        handler.setTarget(target);
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),handler);
    }
}
